5.2.1 Trace algorithms that perform a quicksort on linear arrays.
5.2.2 Construct algorithms that perform a quicksort on linear arrays.
181 recursive algorithm to sort 182 public static void sort(Double a[][], int left, int right, int ind) { 183 if (left >= right) { 184 return; 185 } 186 int k = left; 187 int j = right; 188 double pivotValue = a[ (left + right) / 2][ind]; salient feature 189 while (k < j) { 190 while (a[k][ind] < pivotValue) salient feature (pivot point) 191 { 192 k++; 193 } 194 while (pivotValue < a[j][ind]) { 195 j--; 196 } 197 if (k <= j) { 198 Double[] temp = a[k]; swap a[k] and a[j] 199 a[k] = a[j]; 200 a[j] = temp; 201 k++; 202 j--; 203 } 204 } 205 sort(a, left, j, ind); salient feature (recursion) 206 sort(a, k, right, ind); 207 }
5.2.3 Construct a hash table including the generation of addresses using
modulo arithmetic and the handling of clashes by locating next free
space.
publicclass HashTable {/**
* HashTable
* Demonstrates the use of a hash function
* to calculate the memory address for a product code.
*
* @RNJ
* @10/08/09
*/// declare the array to represent memory addressespublicstaticString[][] memory =newString[103][20];publicstaticvoid main (String arg[]){for(int i =0; i <103; i ++){for(int z =0; z <20; z++){
memory[i][z]=null;}}new HashTable();}public HashTable(){String productCode;int menustring =0;// an introduction
IBIO.output("HASH TABLE");
IBIO.output("Takes a product code to be hashed and stored");while(menustring !=3){
IBIO.output("");
IBIO.output(" ---- The Hasher ----");
IBIO.output("");
IBIO.output("Type 1 to add a student.");
IBIO.output("Type 2 to show .");
IBIO.output("Type 3 to close the program.");
IBIO.output("");
menustring = IBIO.inputInt();if(menustring ==1){
productCode = IBIO.input("Who do you want to add?");int hashCode = hashCode(productCode);if(memory [hashCode][0]==null){
memory [hashCode][0]= productCode;}else{int n =0;while(memory[hashCode][n]!=null){n++;}
memory[hashCode][n]= productCode;
IBIO.output("A collosion has been solved!");}}elseif(menustring ==2){
showMemory();}}}publicint hashCode(String productCode){int sum =0;for(int i =0; i < productCode.length(); i ++){
sum = sum + productCode.charAt(i);}return sum %103;}publicvoid showMemory (){boolean poke;for(int i =0; i <103; i ++){
poke =true;System.out.print(memory[i][0]+"-");if(memory[i][0]!=null){for(int z =1; z <20; z++){if(memory[i][z]==null){}else{System.out.println(memory[i][z]+"-");}}}}}}
5.2.4 Trace algorithms that implement a stack in an array.
5.2.5 Construct algorithms that implement a stack in an array. This includes: to initialize a stack, to test for an empty or a full stack, to push a data item, to pop a data item and to display the top dataitem. All operations must protect against overflow and underflow.
publicclassStack{publicstaticvoid main(String[] args){int size = IBIO.inputInt("How many members do you want your stack to have: ");int top=0;char stacks[]=newchar[size];// an introduction
IBIO.output("___________________");
IBIO.output("Stack's");
IBIO.output("because people like pushing and poping");
IBIO.output(";3");// the choice menuboolean exit =false;while(!exit){
IBIO.output("___________________");
IBIO.output("MENU:");int choice = IBIO.inputInt("Type 1 to pop, Type 2 to push, Type 3 to show");if(choice ==1){if(top ==0){
IBIO.output("Poping is not allowed, thingy is empty");}else{char value = stacks[top -1];
top = top -1;
IBIO.output("Returning: "+ value);}}if(choice ==2){if(top == size){
IBIO.output("Pushing is not allowed, thingy is full");}else{char value = IBIO.inputChar("What do you wanna add to the stacky");
stacks[top]= value;
top = top +1;}}if(choice ==3){for(int i=0; i < top; i++){
IBIO.output(stacks[i]);}}}}}
5.2.6 Trace algorithms that implement a queue in an array.
5.2.7 Construct algorithms that implement a queue in an array.
importjava.io.*;importjava.util.*;importjava.text.*;publicclass Queue
{//Declaring Arraystaticchar[] queue;//Declaring last person in queuestaticint end =0;publicstaticvoid main(String args[]){int size = IBIO.inputInt("How many spots do you want your queue to have?");char[] queue =newchar[size];int choice =0;while(choice !=9){System.out.println("Option 1: Enqueue ");System.out.println("Option 2: Dequeue ");System.out.println("Option 3: Print ");System.out.println("Option 4: Reset ");System.out.println("Option 9: Exit ");
choice = IBIO.inputInt("Choicy choicy time: ");System.out.println("");switch(choice){case1:char intro = IBIO.inputChar("Whatta wanna add: ");if(end == size){
IBIO.output("");
IBIO.output("");
IBIO.output("Queue is full, please unqueue before queueing ");
IBIO.output("");
IBIO.output("");}else{
queue[end]= intro;
end = end +1;break;}break;case2:if(end ==0){
IBIO.output("");
IBIO.output("");
IBIO.output("Queue is emptyyyyyy, please queue before unqueueing ");
IBIO.output("");
IBIO.output("");}else{
IBIO.output("");
IBIO.output("");
IBIO.output("Character (cause it really isn't a person: "+ queue[end -1]+"has 'left' the queue ");
IBIO.output("");
IBIO.output("");
end = end -1;}break;case3:for(int i =0; i < end; i++){
IBIO.output(queue[i]);}break;case4:
end =0;break;default:System.out.println("Please only write 1,2,3 or 4");}}}}
5.2.2 Construct algorithms that perform a quicksort on linear arrays.
181 recursive algorithm to sort
182 public static void sort(Double a[][], int left, int right, int ind) {
183 if (left >= right) {
184 return;
185 }
186 int k = left;
187 int j = right;
188 double pivotValue = a[ (left + right) / 2][ind]; salient feature
189 while (k < j) {
190 while (a[k][ind] < pivotValue) salient feature (pivot point)
191 {
192 k++;
193 }
194 while (pivotValue < a[j][ind]) {
195 j--;
196 }
197 if (k <= j) {
198 Double[] temp = a[k]; swap a[k] and a[j]
199 a[k] = a[j];
200 a[j] = temp;
201 k++;
202 j--;
203 }
204 }
205 sort(a, left, j, ind); salient feature (recursion)
206 sort(a, k, right, ind);
207 }
5.2.3 Construct a hash table including the generation of addresses using
modulo arithmetic and the handling of clashes by locating next free
space.
5.2.4 Trace algorithms that implement a stack in an array.
5.2.5 Construct algorithms that implement a stack in an array. This includes: to initialize a stack, to test for an empty or a full stack, to push a data item, to pop a data item and to display the top dataitem. All operations must protect against overflow and underflow.
5.2.6 Trace algorithms that implement a queue in an array.
5.2.7 Construct algorithms that implement a queue in an array.